//
//  DataProcess.m
//  KidReading
//
//  Created by Telen on 16/4/10.
//  Copyright © 2016年 刘赞黄Telen. All rights reserved.
//

#import "DataProcess.h"

@implementation DataProcess

+ (BOOL)iOS7_low
{
    return [[[UIDevice currentDevice] systemVersion] floatValue] < 8 ;
}

+ (BOOL)iOS9_low
{
    return [[[UIDevice currentDevice] systemVersion] floatValue] < 9 ;
}

+ (CGRect)exchangeWH:(CGRect)frame
{
    CGFloat temp = frame.size.height;
    frame.size.height = frame.size.width;
    frame.size.width = temp;
    return frame;
}

+ (bool)haveChinesWords:(NSString *)str
{
    //判断是否有中文
    for(int i=0; i< [str length];i++){
        unichar a = [str characterAtIndex:i];
        if( a >= 0x4e00 && a <= 0x9fff)
        {
            return YES;
        }
        
    }
    return NO;
}

+ (bool)onlyChinesWords:(NSString *)str
{
    if (str.length == 0) {
        return NO;
    }
    //判断是否有中文
    for(int i=0; i< [str length];i++){
        unichar a = [str characterAtIndex:i];
        if(!(a >= 0x4e00 && a <= 0x9fff))
        {
            return NO;
        }
    }
    return YES;
}

+ (void)do_Main:(void (^)(void))did delay:(double)delay
{
    @autoreleasepool {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay*NSEC_PER_SEC), dispatch_get_main_queue(), did);
    }
}
+ (void)do_Main_quick:(void(^)(void))did
{
    [self do_Main:did delay:0.001];
}

+ (void)do_Async:(void (^)(void))did delay:(double)delay
{
    @autoreleasepool {
        //dispatch_queue_t do_async_queue = dispatch_queue_create("do_Async", DISPATCH_QUEUE_CONCURRENT);
        dispatch_queue_t do_async_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay*NSEC_PER_SEC), do_async_queue, did);
    }
    //[self do_Async_low:did delay:delay];
}
+ (void)do_Async_quick:(void (^)(void))did
{
    [self do_Async:did delay:0.001];
}

+ (void)do_Async_default:(void (^)(void))did delay:(double)delay
{
    @autoreleasepool {
        dispatch_queue_t do_async_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay*NSEC_PER_SEC), do_async_queue, did);
    }
}

+ (void)do_Async_low:(void (^)(void))did delay:(double)delay
{
    @autoreleasepool {
        dispatch_queue_t do_async_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay*NSEC_PER_SEC), do_async_queue, did);
    }
}

+ (void)do_Async_high:(void (^)(void))did delay:(double)delay
{
    @autoreleasepool {
        dispatch_queue_t do_async_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay*NSEC_PER_SEC), do_async_queue, did);
    }
}

+ (void)do_Sync:(void(^)(void))did {
    @autoreleasepool {
        dispatch_queue_t  queue = dispatch_queue_create([[NSString stringWithFormat:@"dosync.%@", self] UTF8String], NULL);
        dispatch_sync(queue, did);
    }
}

+ (void)do_Async:(void (^)(void))did delay:(double)delay mainCallback:(void (^)(void))cmp
{
    @autoreleasepool {
        dispatch_queue_t do_async_queue = dispatch_queue_create("do_Async", DISPATCH_QUEUE_CONCURRENT);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay*NSEC_PER_SEC), do_async_queue, ^{
            did();
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                cmp();
            });
        });
    }
}

//系统空间容量
+ (NSNumber *)totalDiskSpace
{
    NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
    return [fattributes objectForKey:NSFileSystemSize];
}
+ (NSString *)totalDiskSpace_GB
{
    NSNumber* size = [self totalDiskSpace];
    unsigned long long fileSize = size.unsignedLongLongValue;
    NSString* cacheString = [NSString stringWithFormat:@"%.2f GB",fileSize/(1024.0*1024*1024)];
    return cacheString;
}

+ (NSNumber *)freeDiskSpace
{
    NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
    return [fattributes objectForKey:NSFileSystemFreeSize];
}

+ (NSString *)freeDiskSpace_GB
{
    NSNumber* size = [self freeDiskSpace];
    unsigned long long fileSize = size.unsignedLongLongValue;
    NSString* cacheString = [NSString stringWithFormat:@"%.2f GB",fileSize/(1024.0*1024*1024)];
    return cacheString;
}

+(NSNumber*)getCacheSizeForPath:(NSArray*)abPathArr
{
    
    //NSDate* begin = [NSDate date];
    NSFileManager* fm = [NSFileManager defaultManager];
    __block NSError* error = nil;
    __block unsigned long long fileSize = 0;
    
    //
    @autoreleasepool {
        dispatch_queue_t  queue = dispatch_queue_create([[NSString stringWithFormat:@"CacheSize.%@", self] UTF8String], NULL);
        
        //
        NSString*(^fullPath)(NSString*,NSString*) = ^(NSString*abPath,NSString* filename){
            return [abPath stringByAppendingPathComponent:filename];
        };
        
        //获取文件夹的缓存
        for (NSString* abPath in abPathArr) {
            dispatch_sync(queue, ^{
                //NSArray* subFiles = [fm subpathsAtPath:abPath];//此方法递归，慢
                NSArray* subFiles = [fm subpathsOfDirectoryAtPath:abPath error:&error];
                //NSLog(@"subpath = %@",subFiles);
                for (NSString* fileName in subFiles) {
                    //if ([fileName hasSuffix:@"png"]||[fileName hasSuffix:@"jpg"]) {
                    NSDictionary* dic = [fm attributesOfItemAtPath:fullPath(abPath,fileName) error:&error];
                    unsigned long long size = (error ? 0:[dic fileSize]);
                    fileSize += size;
                    //}
                }
            });
        }
    }
    
    //NSTimeInterval time = [begin timeIntervalSinceNow];
    //NSLog(@"遍历文件耗费时间:%lf",time);
    return @(fileSize);
}

+ (BOOL)hasCacheSize_MB_ForPath:(NSArray *)abPathArr
{
    __block BOOL has = NO;
    __block NSError* error = nil;
    __block unsigned long long fileSize = 0;
    NSFileManager* fm = [NSFileManager defaultManager];
    //
    @autoreleasepool {
        dispatch_queue_t  queue = dispatch_queue_create([[NSString stringWithFormat:@"CacheSize.%@", self] UTF8String], NULL);
        
        //
        NSString*(^fullPath)(NSString*,NSString*) = ^(NSString*abPath,NSString* filename){
            return [abPath stringByAppendingPathComponent:filename];
        };
        
        //获取文件夹的缓存
        for (NSString* abPath in abPathArr) {
            dispatch_sync(queue, ^{
                //NSArray* subFiles = [fm subpathsAtPath:abPath];//此方法递归，慢
                NSArray* subFiles = [fm subpathsOfDirectoryAtPath:abPath error:&error];
                //NSLog(@"subpath = %@",subFiles);
                for (NSString* fileName in subFiles) {
                    //if ([fileName hasSuffix:@"png"]||[fileName hasSuffix:@"jpg"]) {
                    NSDictionary* dic = [fm attributesOfItemAtPath:fullPath(abPath,fileName) error:&error];
                    unsigned long long size = (error ? 0:[dic fileSize]);
                    fileSize += size;
                    if (fileSize > 0) {
                        has = YES;
                        break;
                    }
                    //}
                }
            });
        }
    }
    return has;
}

+ (NSString *)getCacheSize_MB_ForPath:(NSArray *)abPathArr
{
    NSNumber* size = [self getCacheSizeForPath:abPathArr];
    unsigned long long fileSize = size.unsignedLongLongValue;
    NSString* cacheString = [NSString stringWithFormat:@"%.2f MB",fileSize/(1024.0*1024)];
    return cacheString;
}

+ (double)getFileSize_MB:(NSString *)filePath
{
    NSFileManager* fm = [NSFileManager defaultManager];
    if ([fm fileExistsAtPath:filePath]) {
        NSError* error = nil;
        NSDictionary* dic = [fm attributesOfItemAtPath:filePath error:&error];
        unsigned long long size = (error ? 0:[dic fileSize]);
        double fsize = size/(1024.0*1024);
        return fsize;
    }
    return -1;
}

+ (NSString *)sizeStringByFormattingBytes:(unsigned long long)bytes
{
    NSArray *units = @[@"%1.0f Bytes", @"%1.2f KB", @"%1.2f MB", @"%1.2f GB", @"%1.2f TB"];
    unsigned long long value = bytes * 10;
    for (NSUInteger i=0; i<[units count]; i++)
    {
        if (i > 0)
        {
            value = value/1024;
        }
        if (value < 10000)
        {
            return [NSString stringWithFormat:units[i], value/10.0];
        }
    }
    return [NSString stringWithFormat:units[[units count]-1], value/10.0];
}

@end
